package football

import (
	"encoding/json"
	"fmt"
	"longmen/server/config/global"
	"longmen/server/internal/services/nami/utils"
	"longmen/server/pkg/db/mysql/models/models_sports"
	"time"
)

const oddsListKey = "football_odds:%d"

func getOddsKey(matchId int) string {
	return fmt.Sprintf(oddsListKey, matchId)
}

func fbOddsAdds(matchId int, data []models_sports.FootballOdds) error {
	rds := global.GetRds()
	key := getOddsKey(matchId)
	list := make([]string, 0)
	for _, datum := range data {
		//global.Log.Info("insert redis: ", datum)
		marshal, _ := json.Marshal(datum)
		list = append(list, string(marshal))
	}

	err := rds.LPush(key, list).Err()
	if err != nil {
		return err
	}
	return rds.Expire(key, time.Hour*2).Err()
}

func fbOddsList(matchId int) []models_sports.FootballOdds {
	rds := global.GetRds()
	key := getOddsKey(matchId)
	odds := make([]models_sports.FootballOdds, 0)
	data, err := rds.LRange(key, 0, -1).Result()
	if err != nil {
		return odds
	}
	for _, datum := range data {
		temp := models_sports.FootballOdds{}
		err := json.Unmarshal([]byte(datum), &temp)
		if err != nil {
			continue
		}
		odds = append(odds, temp)
	}
	return odds
}
func ShardingCreate(matchId int, odds []models_sports.FootballOdds, batchSize int) {
	records := fbOddsList(matchId)
	if len(records) == 0 {
		global.SportSharding().Where("match_id = ?", matchId).Find(&records)
	}
	insertRecords := make([]models_sports.FootballOdds, 0)
	if len(records) > 0 {
		for _, newRecord := range odds {
			exists := false
			for _, oldRecord := range records {
				if newRecord.MatchId == oldRecord.MatchId &&
					newRecord.MatchStatus == oldRecord.MatchStatus &&
					newRecord.CompanyId == oldRecord.CompanyId &&
					newRecord.CompetitionTime == oldRecord.CompetitionTime &&
					newRecord.Odds1.Equals(oldRecord.Odds1) &&
					newRecord.Odds2.Equals(oldRecord.Odds2) &&
					newRecord.Odds3.Equals(oldRecord.Odds3) &&
					//newRecord.Odds2 == oldRecord.Odds2 &&
					//newRecord.Odds3 == oldRecord.Odds3 &&
					newRecord.OddsStatus == oldRecord.OddsStatus &&
					newRecord.ExponentialType == oldRecord.ExponentialType &&
					newRecord.ExponentialScore == oldRecord.ExponentialScore &&
					newRecord.ChangeTime == oldRecord.ChangeTime {
					exists = true
					// 相同的数据，已经在数据库里有记录了
					break
				}
			}
			if !exists {
				// 数据库里没记录的数据写入
				insertRecords = append(insertRecords, newRecord)
			}

		}
	} else {
		insertRecords = append(insertRecords, odds...)
	}

	// 赛前数据
	preRaceOdds := make([]models_sports.FootballOdds, 0)
	// 赛中数据
	inRaceOdds := make([]models_sports.FootballOdds, 0)
	rds := global.GetRds()

	for _, record := range insertRecords {

		if record.CompetitionTime == "" {
			// 一小时/次
			//key := fmt.Sprintf("football_before:%d", record.MatchId)
			//timeStr := timehelper.Unix(record.ChangeTime, 0).Format("2006010215")
			//field := fmt.Sprintf("%s_%d_%s", record.ExponentialType, record.CompanyId, timeStr)
			//exists := rds.HExists(key, field)
			//if !exists.Val() {
			//	rds.HSet(key, field, 1)
			//	// 存7天
			//	rds.Expire(key, timehelper.Hour*24*7)
			//	preRaceOdds = append(preRaceOdds, record)
			//}
			preRaceOdds = append(preRaceOdds, record)
		} else {
			// 一分钟/次
			key := fmt.Sprintf("football_after:%d", record.MatchId)
			timeStr := time.Unix(record.ChangeTime, 0).Format("200601021504")
			field := fmt.Sprintf("%s_%d_%s", record.ExponentialType, record.CompanyId, timeStr)
			exists := rds.HExists(key, field)
			if !exists.Val() {
				rds.HSet(key, field, 1)
				// 存24小时
				rds.Expire(key, time.Second*86400)
				inRaceOdds = append(inRaceOdds, record)
			}
		}
	}

	if len(preRaceOdds) > 0 {
		fbOddsAdds(matchId, preRaceOdds)
		utils.GetShardingBatchCreateInstance().CreteFootballOdds(preRaceOdds, batchSize)
	}

	if len(inRaceOdds) > 0 {
		fbOddsAdds(matchId, inRaceOdds)
		utils.GetShardingBatchCreateInstance().CreteFootballOdds(inRaceOdds, batchSize)
	}
}
